package com.easthome.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.easthome.auth.service.SysUserService;
import com.easthome.common.jwt.JwtHelper;
import com.easthome.common.jwt.MD5;
import com.easthome.common.result.Result;
import com.easthome.common.result.ResultCodeEnum;
import com.easthome.model.system.SysRole;
import com.easthome.model.system.SysUser;
import com.easthome.vo.system.LoginVo;
import com.easthome.vo.system.RouterVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台登录登出
 * </p>
 */
@Api(tags = "后台登录管理")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;
    /**
     * 登录
     * @return
     */

    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo,HttpServletRequest request) {
//        //1.取出账号密码
//        String username = loginVo.getUsername();
//        String password = loginVo.getPassword();
//        //2.判断账号
//        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(SysUser::getUsername,username);
//        SysUser one = sysUserService.getOne(lambdaQueryWrapper);
//        if(one == null ){
//            return Result.build(ResultCodeEnum.LOGIN_USER_ERROR);
//        }
//
//        if(!one.getPassword().equals(MD5.encrypt(password))){
//            return Result.build(ResultCodeEnum.LOGIN_PASS_ERROR);
//        }
//        if(one.getStatus()!=1){
//            return Result.build(ResultCodeEnum.LOGIN_STATUS_ERROR);
//        }
//        String token = JwtHelper.createToken(one.getId(), username);
//        Map<String, Object> map = new HashMap<>();
//        map.put("token",token);
//        return Result.ok(map);
        return sysUserService.login(loginVo,request);
    }
    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("info")
    public Result info(HttpServletRequest request) {
        String token=request.getParameter("token");
        Long userId = JwtHelper.getUserId(token);
        SysUser sysUser = sysUserService.getById(userId);
        //1.获取用户拥有角色
        List<SysRole> roleList=sysUserService.findRolesByUserId(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("roles",roleList.toString());
        //2.获取名字
        map.put("name",sysUser.getName());
        //3.获取头像
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
        //4.获取3级菜单，即按钮
       List<String> buttons = sysUserService.findButtonsByUserId(userId);
        map.put("buttons", buttons);
        //5.获取路由
        List<RouterVo> routerVoList = sysUserService.findRotersByUserId(userId);
        map.put("routers", routerVoList);

        return Result.ok(map);
    }
    /**
     * 退出
     * @return
     */
    @PostMapping("logout")
    public Result logout(){
        return Result.ok();
    }

    //写的没我好！！！！！！！

}